package main

import (
	"context"
	"fmt"
	_ "gitee.com/jasmine456/devcloud-mini/cmdb/apps"
	"github.com/emicklei/go-restful/v3"
	"github.com/infraboard/mcenter/clients/rpc/middleware"
	"github.com/infraboard/mcenter/clients/rpc/tools"
	"github.com/infraboard/mcube/ioc"
	"github.com/infraboard/mcube/ioc/config/application"
	"net/http"
)

func main() {
	req := ioc.NewLoadConfigRequest()
	// 配置文件默认路径: etc/applicaiton.toml
	req.ConfigFile.Enabled = true
	err := ioc.ConfigIocObject(req)
	if err != nil {
		panic(err)
	}
	application.App().HTTP.RouterBuildConfig.BeforeLoad = func(h http.Handler) {
		//断言router
		r := h.(*restful.Container)
		// 添加中间件，接入到用户中心
		r.Filter(middleware.RestfulServerInterceptor())
	}

	// cmdb如果怎么把功能列表注册给mcenter
	application.App().HTTP.RouterBuildConfig.AfterLoad = func(h http.Handler) {
		//断言router
		r := h.(*restful.Container)
		// 获取到当前加载的所有路由条目，并且通过rpc注册给mcenter服务
		tools.NewEndpointRegister().Registry(context.Background(), r, "0.0.1")
		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Println("注册成功")
		}
	}

	//// 启动补充grpc 认证中间件,启动之前补充认证grpc服务的认证中间件
	//application.App().GRPC.AddInterceptors(middleware.GrpcAuthUnaryServerInterceptor())

	// 启动应用, 应用会自动加载 刚才实现的Gin Api Handler
	// 1. 自动加载 gin api handler
	// 2. 自动加载 grpc handler
	err = application.App().Start(context.Background())

	if err != nil {
		panic(err)
	}
}
